import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JDialogExample extends JFrame {
    public JDialogExample() {
        JButton button = new JButton("Show Login Dialog");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                LoginDialog loginDialog = new LoginDialog(JDialogExample.this);
                loginDialog.setVisible(true);
            }
        });
        add(button);
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }
    class LoginDialog extends JDialog {
        private JTextField usernameField;
        private JPasswordField passwordField;
        public LoginDialog(Frame owner) {
            super(owner, "Login", true);
            JLabel usernameLabel = new JLabel("Username:");
            usernameField = new JTextField(20);
            JPanel usernamePanel = new JPanel(new BorderLayout());
            usernamePanel.add(usernameLabel, BorderLayout.WEST);
            usernamePanel.add(usernameField, BorderLayout.CENTER);
            JLabel passwordLabel = new JLabel("Password:");
            passwordField = new JPasswordField(20);
            JPanel passwordPanel = new JPanel(new BorderLayout());
            passwordPanel.add(passwordLabel, BorderLayout.WEST);
            passwordPanel.add(passwordField, BorderLayout.CENTER);
            JButton okButton = new JButton("OK");
            okButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String username = usernameField.getText();
                    char[] password = passwordField.getPassword();
                    System.out.println("Username: " + username);
                    System.out.println("Password: " + String.valueOf(password));
                    dispose();
                }
            });
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
            buttonPanel.add(okButton);
            JPanel contentPane = new JPanel(new GridLayout(2, 1));
            contentPane.add(usernamePanel);
            contentPane.add(passwordPanel);
            setContentPane(contentPane);
            add(buttonPanel, BorderLayout.SOUTH);
            pack();
            setLocationRelativeTo(owner);
        }
    }
    public static void main(String[] args) {
        new JDialogExample();
    }
}
